package com.squirrel.michale.search5;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/1/18 9:24 PM
 * @company Michale Squirrel
 * @link  https://leetcode.cn/problems/search-in-rotated-sorted-array/
 * @description
 *
 * 33. 搜索旋转排序数组
 * 中等
 * 2.4K
 * 相关企业
 * 整数数组 nums 按升序排列，数组中的值 互不相同 。
 *
 * 在传递给函数之前，nums 在预先未知的某个下标 k（0 <= k < nums.length）上进行了 旋转，使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]（下标 从 0 开始 计数）。例如， [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。
 *
 * 给你 旋转后 的数组 nums 和一个整数 target ，如果 nums 中存在这个目标值 target ，则返回它的下标，否则返回 -1 。
 *
 * 你必须设计一个时间复杂度为 O(log n) 的算法解决此问题。
 *
 * 示例 1：
 *
 * 输入：nums = [4,5,6,7,0,1,2], target = 0
 * 输出：4
 * 示例 2：
 *
 * 输入：nums = [4,5,6,7,0,1,2], target = 3
 * 输出：-1
 * 示例 3：
 *
 * 输入：nums = [1], target = 0
 * 输出：-1
 *
 *
 * 提示：
 *
 * 1 <= nums.length <= 5000
 * -104 <= nums[i] <= 104
 * nums 中的每个值都 独一无二
 * 题目数据保证 nums 在预先未知的某个下标上进行了旋转
 * -104 <= target <= 104
 */
public class LeetCode0033 {

    public int search(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;

        int middle = 0;
        while (left <= right) {
            middle = left + ((right - left) >> 1);
            if (nums[middle] > nums[left]) {
                if (nums[left] == target) {
                    return left;
                } else if (nums[middle] == target) {
                    return middle;
                } else if (nums[right] == target) {
                    return right;
                } else if (nums[left] < target && target < nums[middle]) {
                    right = middle - 1;
                } else if (nums[middle] < target ) {
                    left = middle + 1;
                }  else if (nums[right] < target) {
                    return -1;
                } else if (nums[left] > target) {
                    left = middle + 1;
                }

            } else if (nums[middle] < nums[left]) {

                if (nums[left] == target) {
                    return left;
                } else if (nums[middle] == target) {
                    return middle;
                } else if (nums[right] == target) {
                    return right;
                } else if (nums[middle] > target) {
                    right = middle - 1;
                } else if (nums[middle] < target && target < nums[right]) {
                    left = middle + 1;
                } else if (nums[left] < target) {
                    right = middle - 1;
                } else if (nums[right] < target && target < nums[left]) {
                    return -1;
                }
            } else{
                if (nums[left] == target) {
                    return left;
                } else if (nums[middle] == target) {
                    return middle;
                } else if (nums[right] == target) {
                    return right;
                } else {
                    return -1;
                }
            }
        }
        return -1;
    }


    public int search2(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;

        int middle = 0;
        while (left <= right) {
            middle = left + ((right - left) >> 1);
            if (nums[middle] > nums[left]) {
                if (nums[left] == target) {
                    return left;
                } else if (nums[middle] == target) {
                    return middle;
                } else if (nums[right] == target) {
                    return right;
                } else if (nums[left] < target && target < nums[middle]) {
                    right = middle - 1;
                } else if (nums[middle] < target && target < nums[right]) {
                    left = middle + 1;
                } else if (nums[left] > target) {
                    return -1;
                } else if (nums[right] < target) {
                    return -1;
                }

            } else if (nums[middle] < nums[left]) {

                if (nums[left] == target) {
                    return left;
                } else if (nums[middle] == target) {
                    return middle;
                } else if (nums[right] == target) {
                    return right;
                } else if (nums[middle] > target) {
                    right = middle - 1;
                } else if (nums[middle] < target && target < nums[right]) {
                    left = middle + 1;
                } else if (nums[left] < target) {
                    right = middle - 1;
                } else if (nums[right] < target && target < nums[left]) {
                    return -1;
                }
            }
        }
        return -1;
    }


    /**
     * LeetCode 用例通过后优化
     * @param nums
     * @param target
     * @return
     */
    public int search3(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;

        int middle = 0;
        while (left <= right) {
            middle = left + ((right - left) >> 1);

            if (nums[left] == target) {
                return left;
            } else if (nums[middle] == target) {
                return middle;
            } else if (nums[right] == target) {
                return right;
            }

            if (nums[middle] > nums[left]) {
               if (nums[left] < target && target < nums[middle]) {
                    right = middle - 1;
                } else if (nums[middle] < target || nums[left] > target ) {
                    left = middle + 1;
                }  else if (nums[right] < target) {
                    return -1;
                }

            } else if (nums[middle] < nums[left]) {
                if (nums[middle] > target || nums[left] < target) {
                    right = middle - 1;
                } else if (nums[middle] < target && target < nums[right]) {
                    left = middle + 1;
                } else if (nums[right] < target && target < nums[left]) {
                    return -1;
                }
            } else{
                return -1;
            }
        }
        return -1;
    }


    public int search4(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;

        int middle = 0;
        while (left <= right) {
            middle = left + ((right - left) >> 1);

            if (nums[left] == target) {
                return left;
            } else if (nums[middle] == target) {
                return middle;
            } else if (nums[right] == target) {
                return right;
            }

            if (nums[middle] > nums[left]) {
                if (nums[left] < target && target < nums[middle]) {
                    right = middle - 1;
                } else {
                    left = middle + 1;
                }
            } else if (nums[middle] < nums[left]) {
               if (nums[middle] < target && target < nums[right]) {
                    left = middle + 1;
                } else {
                    right = middle - 1;
                }
            } else {
                return -1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {

        int[] nums = new int[]{4, 5, 6, 7, 0, 1, 2};
        LeetCode0033 leetCode0033 = new LeetCode0033();

        System.out.println(leetCode0033.search(nums, 0));
        System.out.println(leetCode0033.search(nums, 3));

        int[] nums2 = new int[]{1};


        System.out.println(leetCode0033.search(nums2, 0));

        int[] nums3 = new int[]{4, 5, 6, 7, 8, 1, 2, 3};

        System.out.println(leetCode0033.search(nums3, 8));

    }
}
